


import 'package:bike_ride/api/common_repository.dart';
import 'package:bike_ride/common/mvvm/base_view_model.dart';
import 'package:bike_ride/common/util/account_manager.dart';
import 'package:bike_ride/common/widget/notifier_widget.dart';
import 'package:bike_ride/model/mine/model/finance_stat_m.dart';
import 'package:bike_ride/model/unlock/model/bike_info_m.dart';
import 'package:bike_ride/model/unlock/view/unlock_v.dart';
import 'package:dio/dio.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class UnLockViewModel extends PageViewModel<UnLockViewState> {
  CancelToken? cancelToken;


  BikeInfoM? bikeInfoM;
  DiscountStatM? discountStatM;

  bool isError = false;

  @override
  onCreate() {
    cancelToken = CancelToken();
    pageDataModel = PageDataModel();
    getBikeInfo(state.widget.vid);
    getFinanceInfo();
    getBrandAreaInfo();
  }

  @override
  onDispose() {
    if (!(cancelToken?.isCancelled ?? true)) {
      cancelToken?.cancel();
    }
    super.onDispose();
  }


  Future getBikeInfo(String vid) async {
    EasyLoading.show();
    final result = await CommonRepository.getBikeInfoIdl(cancelToken: cancelToken,vid: vid);
    EasyLoading.dismiss();
    if (result != null) {
      bikeInfoM = result;
      if(result.bikeInfo?.areaId != null && result.bikeInfo!.areaId!.isNotEmpty){
        AccountManager.handleAreaCode(tBikeAreaCode: result.bikeInfo?.areaId);
        getBrandAreaInfo();
      }
    }else{
      isError = true;
    }
    state.notifyChange();
  }

  Future getFinanceInfo() async {
    final result = await CommonRepository.getFinanceInfoIdl(cancelToken: cancelToken);
    discountStatM = result;
    state.notifyChange();
  }

  Future getBrandAreaInfo() async {
    final result =
    await CommonRepository.getBrandAreaInfoIdl(cancelToken: cancelToken);
    if (result != null) {
      AccountManager.areaInfoM = result;
    }
    state.notifyChange();
  }

  /// 请求数据
  @override
  Future<PageViewModel?> requestData({Map<String, dynamic>? params}) async {
    return null;

  }
}